package the.broccoli.uc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.broccoli.uc.dto.RoleCondition;
import the.broccoli.uc.dto.UserRoleCondition;
import the.broccoli.uc.entity.Role;
import the.broccoli.uc.entity.UserRole;
import the.broccoli.uc.mapper.UserRoleMapper;
import the.broccoli.uc.service.IRoleService;
import the.broccoli.uc.service.IUserRoleService;

import java.util.*;

/**
 * <p>
 * 用户与角色 服务实现类
 * </p>
 */
@Service
public class UserRoleServiceImpl extends AbstractSuperService<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    private IRoleService roleService;

    /**
     * 保存用户角色信息
     * @param userId        用户ID
     * @param roleIdList    角色ID
     * @param createdId     创建人ID
     * @param createdName   创建人名称
     */
    @Transactional
    @Override
    public void save(String userId, List<String> roleIdList, String createdId, String createdName) {
        baseMapper.deleteByUserId(userId, createdId, createdName, new Date());

        if (!CollectionUtils.isEmpty(roleIdList)) {
            List<UserRole> userRoleList = new ArrayList<>();
            UserRole userRole = null;
            for (String roleId : roleIdList) {
                userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRole.setCreatedId(createdId);
                userRole.setCreatedName(createdName);
                userRole.setCreatedTime(new Date());
                userRoleList.add(userRole);
            }
            saveBatch(userRoleList);
        }
    }

    /**
     * 批量保存用户角色信息
     * @param userIdList    用户ID列表
     * @param roleId        角色ID
     * @param createdId     创建人ID
     * @param createdName   创建人名称
     */
    @Transactional
    @Override
    public void save(List<String> userIdList, String roleId, String createdId, String createdName) {
        baseMapper.deleteByRoleId(roleId, createdId, createdName, new Date());

        if (!CollectionUtils.isEmpty(userIdList)) {
            List<UserRole> userRoleList = new ArrayList<>();
            UserRole userRole = null;
            for (String userId : userIdList) {
                userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRole.setCreatedId(createdId);
                userRole.setCreatedName(createdName);
                userRole.setCreatedTime(new Date());
                userRoleList.add(userRole);
            }
            saveBatch(userRoleList);
        }
    }

    /**
     * 获取用户角色ID
     * @param userId    用户ID
     * @return
     */
    @Override
    public List<String> getRoleIdList(String userId) {
        List<String> data = new ArrayList<>();
        UserRoleCondition condition = new UserRoleCondition();
        condition.setUserId(userId);
        List<UserRole> list = listByCondition(condition);
        if (!CollectionUtils.isEmpty(list)) {
            for (UserRole userRole : list) {
                data.add(userRole.getRoleId());
            }
        }
        return data;
    }



    /**
     * 获取用户关联的所有角色编码
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public Set<String> getRoleCodeList(String userId) {
        RoleCondition condition = new RoleCondition();
        condition.setRoleIdList(getRoleIdList(userId));
        List<Role> roleList = roleService.listByCondition(condition);

        Set<String> roleCodeSet = null;
        if (!CollectionUtils.isEmpty(roleList)) {
            roleCodeSet = new HashSet<>();

            for (Role role : roleList) {
                roleCodeSet.add(role.getRoleCode());
            }
        }
        return roleCodeSet;
    }

    /**
     * 获取角色关联的用户
     * @param roleId    角色ID
     * @return
     */
    @Override
    public List<UserRole> listByRoleId(String roleId) {
        UserRoleCondition condition = new UserRoleCondition();
        condition.setRoleId(roleId);
        return listByCondition(condition);
    }
}
